import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express'; // Express平台适配器，用于处理静态文件和其他Express特性
import { ConfigService } from '@nestjs/config'; // 用于访问配置值的服务
import { ResponseInterceptor } from '../../../common/response.intercept';
import { HttpFilter } from '../../../common/filter.intercept';

import { AppModule } from './app.module';
import { TestAppModule } from '../../test-app/src/test-app.module';

async function bootstrap() {
  // 创建应用程序上下文以访问模块和服务
  const appContext = await NestFactory.createApplicationContext(TestAppModule);

  // 获取ConfigService实例以获取配置值
  const configService = appContext.get(ConfigService);

  // 从配置中获取主机和端口
  const port = configService.get<number>('MAIN_APP_PORT');

  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      // 验证管道
      whitelist: true,
      forbidNonWhitelisted: true, // 白名单之外的参数报错
    }),
  ); // 添加这行启用全局验证管道
  app.useStaticAssets('assets', { prefix: '/assets' });
  app.useGlobalInterceptors(new ResponseInterceptor());
  app.useGlobalFilters(new HttpFilter());
  await app.listen(port);
}
bootstrap();
